package top.itpath.admin.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.itpath.admin.dto.LoginInfo;
import top.itpath.admin.dto.TokenInfo;
import top.itpath.admin.service.ILoginService;
import top.itpath.common.Result;
import top.itpath.common.util.RequestUtil;

/**
 * 系统/认证
 *
 * @author zhaomengru
 * create by  2023/10/10 - 11:28
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/auth")
public class LoginController {

    private final ILoginService loginService;
    private final StringRedisTemplate redisTemplate;

    /**
     * 登录
     *
     * @param loginInfo 登录信息
     * @return token
     */
    @PostMapping("/login")
    Result<TokenInfo> login(@RequestBody LoginInfo loginInfo) {
        return Result.success(loginService.login(loginInfo.getUsername(), loginInfo.getPassword()));
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    Result<?> logout() {
        redisTemplate.opsForSet().add(TokenInfo.BLOCK_TOKEN, RequestUtil.getToken());
        return Result.success().setMessage("logout success");
    }

    /**
     * 刷新token
     *
     * @return token
     */
    @PostMapping("/refreshToken")
    Result<TokenInfo> refreshToken() {
        return Result.success(loginService.refreshToken());
    }
}
